/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "dataeditor.h"
#include "projectmapper.h"
#include "objectmapper.h"
#include <projectmodellist.h>
#include <QtDeclarative/QDeclarativeContext>
#include <QtDeclarative/QDeclarativeEngine>

using namespace Try;

DataEditor::DataEditor(QWidget* parent)
    :   QDeclarativeView(parent),
        m_activeMapper(0)
{
    this->setObjectName("dataEditor");
    this->setEnabled(false);

    this->setWindowTitle(tr("Data Editor"));
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    this->setStyleSheet(QString("background: transparent"));
    this->setResizeMode(QDeclarativeView::SizeRootObjectToView);

    QObject::connect(ProjectModelList::instance(), SIGNAL(projectLoaded(Project*)), this, SLOT(onProjectLoaded(Project*)));
    QObject::connect(ProjectModelList::instance(), SIGNAL(projectClosed(Project*)), this, SLOT(onProjectClosed(Project*)));
    QObject::connect(ProjectModelList::instance(), SIGNAL(projectSelectionChanged(Project*)), this, SLOT(onProjectSelectionChanged(Project*)));

    this->refresh();

    this->setSource(QUrl("data/ui/DataEditor.qml"));
}

DataEditor::~DataEditor()
{
    this->clear();
}

void DataEditor::setActiveMapper(DataMapper* mapper)
{
    if (m_activeMapper != mapper)
    {
        if (m_activeMapper)
            delete m_activeMapper;

        m_activeMapper = mapper;

        if (m_activeMapper)
            QObject::connect(m_activeMapper, SIGNAL(propertyChanged(QString)), this, SLOT(onPropertyChanged(QString)));

        this->refresh();
    }
}

void DataEditor::refresh()
{
    QDeclarativeEngine* engine = this->engine();
    QDeclarativeContext* ctxt = engine->rootContext();

    ctxt->setContextProperty("widget", this);
    ctxt->setContextProperty("mapper", m_activeMapper);
}

void DataEditor::clear()
{
    this->setActiveMapper(0);
}

void DataEditor::onProjectLoaded(Project* project)
{
    if (project)
        this->setEnabled(true);

    if (!ProjectMapper::hasInstance())
        new ProjectMapper(project, this);

    this->setActiveMapper(ProjectMapper::instance());
}

void DataEditor::onProjectClosed(Project* project)
{
    this->clear();

    if (project)
        this->setEnabled(false);
}

void DataEditor::onProjectSelectionChanged(Project* project)
{
    if (project)
    {
        ObjectList selected_objects = project->selectedObjects();

        if (selected_objects.size() > 0)
        {
            Object* object = selected_objects.at(0);

            if (!ObjectMapper::hasInstance())
                new ObjectMapper(object, this);

            this->setActiveMapper(ObjectMapper::instance());
        }
        else
        {
            this->onProjectLoaded(project);
        }
    }
}

void DataEditor::onPropertyChanged(const QString& name)
{
    // TODO.
}
